package com.hotelManagerSystem.service.impl;

import com.hotelManagerSystem.model.DishOrder;
import com.hotelManagerSystem.mapper.DishOrderMapper;
import com.hotelManagerSystem.service.IDishOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author treasy
 * @since 2021-12-22
 */
@Service
public class DishOrderServiceImpl extends ServiceImpl<DishOrderMapper, DishOrder> implements IDishOrderService {
    @Autowired
    DishOrderMapper dishOrderMapper;

    @Override
    public Map<String, Object> addOne(DishOrder dishOrder) {
        Map<String, Object> map = new HashMap<>();
        //当前订单的时间
        Date date = new Date();
        //将当前订单的时间转换为LocalDateTime
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        dishOrder.setOrderTime(localDateTime);
        int flag = dishOrderMapper.insert(dishOrder);
        if(flag == 1) {
            map.put("code", 500);
            map.put("addInfo", "添加失败");
        } else {
            map.put("code", 200);
            map.put("addInfo", "添加成功");
        }
        return map;
    }

    //查询全部
    @Override
    public Map<String, Object> queryAll() {
        List<DishOrder> list = dishOrderMapper.queryAll();
        Map<String, Object> map = new HashMap<>();
        map.put("dishOrderList", list);
        return map;
    }

    @Override
    public Map<String, Object> delOne(Integer id) {
        int flag = dishOrderMapper.deleteById(id);
        Map<String, Object> map = new HashMap<>();
        if(flag == 1) {
            map.put("delInfo", "删除成功");
            map.put("code", 200);
        } else {
            map.put("delInfo", "删除失败");
            map.put("code", 500);
        }
        return map;
    }

    @Override
    public Map<String, Object> queryByRoomId(Integer id) {
        Map<String, Object> map = new HashMap<>();
        List<DishOrder> list = dishOrderMapper.queryByRoomId(id);
        map.put("dishOrderList", list);
        return map;
    }
}
